package com.luo.dt.seata.tcc.service.impl;

import com.luo.dt.common.model.result.RespResult;
import com.luo.dt.seata.tcc.model.dto.BusinessDto;
import com.luo.dt.seata.tcc.service.BusinessLocalTccService;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 业务服务本地TCC实现类
 *
 * @author luohq
 * @date 2022-12-04 13:51
 */
@Service
@Slf4j
public class BusinessLocalTccServiceImpl implements BusinessLocalTccService {

    @Override
    public RespResult tryBusiness(BusinessDto businessDto) {
        log.info("开始TCC分支事务，XID={}", RootContext.getXID());
        //此处对businessDto进行修改后，在后续commit方法中获取到的参数仍是修改之前的参数
        //businessDto.setCount(businessDto.getCount() + 1);
        return RespResult.success();
    }

    @Override
    public Boolean commitBusiness(BusinessActionContext context) {
        log.info("TCC提交成功, XID={}, businessDto={}", context.getXid(), context.getActionContext("businessDto"));
        return true;
    }

    @Override
    public Boolean cancelBusiness(BusinessActionContext context) {
        log.warn("TCC回滚业务, XID={}, businessDto={}", context.getXid(), context.getActionContext("businessDto"));
        return true;
    }
}

